function getParam(name){  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec(window.location.href);
    if(results == null)
        return "";  
    else    
        return results[1];
}

var projecName;
var namesProjects;
var originalTaskName;

function displayBackButton(){
    projectName = getParam("projectName");
    namesProjects = getParam("projects");
    document.write("<a data-theme=\"a\" data-mini=\"true\" data-inline=\"true\" data-icon=\"back\" data-role=\"button\" rel=\"external\" href=\"cronograma.jsp?projectName=" + projectName + "&projects=" + namesProjects + "\"> Atr&aacute;s </a>");
}

function loadShowData(){
    var taskInfo = getParam("taskInfo");
    var properties = taskInfo.toString().split('||');
    
    var taskName = properties[0];
    originalTaskName = taskName;
    var taskDescription = properties[1];
    var taskStartDate = properties[2];
    var taskFinishDate = properties[3];     
    var taskPercentage = properties[4];
    
    var inputNombre =     document.getElementById("nombre"); 
    var inputDescrip =    document.getElementById("descrip");
    var inputFecha_ini =  document.getElementById("fecha_ini");
    var inputFecha_fin =  document.getElementById("fecha_fin");
    var inputPercentage = document.getElementById("porcent");
    
    inputNombre.value = taskName;
    inputDescrip.value = taskDescription;
    inputFecha_ini.value = taskStartDate;
    inputFecha_fin.value = taskFinishDate;
    inputPercentage.value = taskPercentage;
}

function updateTask(){
    projectName = getParam("projectName");
    var projectText = Android.loadProjectWithName(projectName);
    
    var nombre = document.getElementById("nombre").value;
    var descrip = document.getElementById("descrip").value;
    var fecha_ini = document.getElementById("fecha_ini").value;
    var fecha_fin = document.getElementById("fecha_fin").value;
    var porcent = document.getElementById("porcent").value;
    
    var parrafoErrores = document.getElementById("parrafoErrores");
    parrafoErrores.innerHTML = "";
    
    if(nombre == null || nombre == ""){
        parrafoErrores.innerHTML = "No se ha ingresado el nombre";
        return;
    }
    
    if(nombre.indexOf(" ") != -1){
        parrafoErrores.innerHTML = "El nombre no puede contener espacios";
        return;
    }
    
    if(descrip == null || descrip == ""){
        parrafoErrores.innerHTML = "No se ha ingresado una descripcion";
        return;
    }
    
    if(fecha_ini == null || fecha_ini == ""){
        parrafoErrores.innerHTML = "No se ha seleccionado la fecha inicio";
        return;
    }
    
    if(fecha_fin == null || fecha_fin== ""){
        parrafoErrores.innerHTML = "No se ha seleccionado la fecha fin";
        return;
    }
    
    var properties = projectText.toString().split('||');
    
    var projectName = properties[0];
    var projectDescription = properties[1];
    var projectStartDate = properties[2];
    var projectFinishDate = properties[3]; 
    var projectTime = properties[4];
    var taskList = properties[5].split('--');
    
    var modifiedTask = nombre+"|"+descrip+"|"+fecha_ini+"|"+fecha_fin+"|0|0|"+porcent;
    var newTaskList = "";
    
    var length = taskList.length;
   
    for(i=0; i<length; i++){            
        var array = taskList[i].split('|');
        
        if(array[0] == originalTaskName){
            newTaskList = newTaskList.concat(modifiedTask);
        }else{
            newTaskList = newTaskList.concat(taskList[i]);
        }
        
        if (i < length-1){
            newTaskList = newTaskList.concat('--');
        }
        
    }
        
    var project = projectName+"||"+projectDescription+"||"+projectStartDate+"||"+projectFinishDate+"||"+projectTime+"||"+newTaskList;
    
    Android.saveProject(project); 
    
    // redirecting back
    namesProjects = getParam("projects");
    window.location.href = "file:///android_asset/www/web/cronograma.jsp?projectName="+projectName + "&projects=" + namesProjects;
}

function deleteTask(){
    projectName = getParam("projectName");
    var projectText = Android.loadProjectWithName(projectName);
    
    var properties = projectText.toString().split('||');
    
    var projectName = properties[0];
    var projectDescription = properties[1];
    var projectStartDate = properties[2];
    var projectFinishDate = properties[3]; 
    var projectTime = properties[4];
    var taskList = properties[5].split('--');
    
    var newTaskList = "";
    
    var length = taskList.length;
   
    for(i=0; i<length; i++){            
        var array = taskList[i].split('|');
        
        if(array[0] != originalTaskName){
            newTaskList = newTaskList.concat(taskList[i]);
            if (i < length-1){
                newTaskList = newTaskList.concat('--');
            }
        }
    }
        
    var project = projectName+"||"+projectDescription+"||"+projectStartDate+"||"+projectFinishDate+"||"+projectTime+"||"+newTaskList;
    
    Android.saveProject(project); 
    
    // redirecting back
    namesProjects = getParam("projects");
    window.location.href = "file:///android_asset/www/web/cronograma.jsp?projectName="+projectName + "&projects=" + namesProjects;
}